package com.mb.app.controller;

import com.mb.app.domain.TbOrderReturn1;
import com.mb.app.domain.TbReturnOrderItem;
import com.mb.app.service.ITbOrderReturn1Service;
import com.mb.app.service.ITbReturnOrderItemService;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单退款信息  前端控制器
 * </p>
 *
 * @author zwl
 * @since 2019-04-22
 */
@Controller
@RequestMapping("/app/orderReturn")
public class TbOrderReturnController {

    
    @Autowired
    private ITbOrderReturn1Service orderReturn1Service;

    @Autowired
    private ITbReturnOrderItemService returnOrderItemService;

    @GetMapping("/index")
    String index(){
        return "/app/orderReturn/orderReturn_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String reqReturnTime = (String) params.get("reqReturnTime");
        if (StringUtils.isNotEmpty(reqReturnTime)) {
            params.put("startTime", DateUtils.parse(reqReturnTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",reqReturnTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbOrderReturn1> list = orderReturn1Service.queryList(query);
        int total = orderReturn1Service.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/dtl/{returnOrderId}")
    String dtl(@PathVariable("returnOrderId") String returnOrderId, Model model){
        model.addAttribute("orderReturnInfo",orderReturn1Service.getDtl(returnOrderId));
        return "/app/orderReturn/orderReturn_info";
    }

    @GetMapping("/edit/{orderReturnId}")
    String edit(@PathVariable("orderReturnId") String orderReturnId, Model model){
        model.addAttribute("orderReturnInfo",orderReturn1Service.getDtl(orderReturnId));
        return "/app/orderReturn/orderReturn_edit";
    }


    @ResponseBody
    @PostMapping("/update")
    public R update(TbOrderReturn1 orderReturn1, HttpServletRequest request){
        int result = orderReturn1Service.update(orderReturn1);
        return R.ok();
    }

    /****************************************************************************************************************************************************************************/
    @ResponseBody
    @GetMapping("/orderItemlist")
    public PageUtils orderItemlist(@RequestParam Map<String, Object> params) {
        // 查询列表数据
//        Query query = new Query(params);
        List<TbReturnOrderItem> list = returnOrderItemService.queryList(params);
        PageUtils pageUtils = new PageUtils(list, list.size());
        return pageUtils;
    }


    /********************************************************************参数信息维护************************************************************************************************/
    //订单人信息
    @GetMapping("/indexPar")
    String indexPar(){
        return "/app/orderReturn/paramConfig_list";
    }

    @ResponseBody
    @GetMapping("/listPar")
    public PageUtils listPar(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<HashMap<String , Object>> listPar = orderReturn1Service.queryListPar(query);
        int total = orderReturn1Service.countPar(query);
        PageUtils pageUtils = new PageUtils(listPar, total);
        return pageUtils;
    }

    @GetMapping("/editPar/{id}")
    String editPar(@PathVariable("id") String id, Model model){
        Map<String , Object> paramConfig = orderReturn1Service.selectParAll(id);
        model.addAttribute("paramConfig",paramConfig);
        return "/app/orderReturn/paramConfig_editPar";
    }

    @ResponseBody
    @PostMapping("/updatePar")
    public R updatePar(@RequestParam Map<String , Object> map, HttpServletRequest request){
        int result = orderReturn1Service.updatePar(map);
        return R.ok();
    }

}
